package com {
	import com.utils.File;
	import com.utils.LoadResponder;
	import com.utils.URI;
	import com.utils.oObject;
	import com.views.structure.Structure;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;

	/**
	 * 视图层基类
	 * @author Liseen
	 */
	public class ViewBase extends SuperBase {
		protected var _stage:Stage;
		protected var _data:Data;
		protected var _control:Controller;
		protected var _lang:Lang;
		protected var _structure:Structure;
		private var _assets:Object;
		private var _libs:Object;
		private var _positionList:Object;

		public function ViewBase(stage:Stage) {
			this._positionList = {};
			this._assets = {};
			this._libs = {};
			this._stage = stage;
			super();
		}

		/**
		 * 获取舞台
		 */
		public function get stage():Stage {
			return this._stage
		}

		/**
		 * 返回视图层架构
		 */
		public function get structure():Structure {
			return this._structure;
		}

		/**
		 * 视图初始化
		 * @param	data	数据层引用
		 * @param	control	控制层引用
		 * @param	lang	语言引用
		 */
		public function init(data:Data, control:Controller, lang:Lang):void {
			this._data = data;
			this._control = control;
			this._lang = lang;
			this._structure = Structure.getInstance(this._stage, this);
			this._stage.addEventListener(Event.RESIZE, resetPosition);
		}

		protected function resetPosition(event:Event):void {
			var _key:String;
			for (_key in this._positionList) {
				this._positionList[_key]();
			}
			;
		}

		protected function createObject(_arg1:Class, _arg2:String = ""):Object {
			_arg2 = (_arg1 + "").replace(/View\]$/, "");
			return (createObjectBase(_arg1, _arg2, (this as View), this._control, this._data));
		}

		/**
		 * 添加模块到资源池中
		 * @param	assetsKey		资源键索引
		 * @param	url				资源路径
		 */
		public function addAssets(assetsKey:String, url:String):void {
			if (this.hasAssets(assetsKey)) {
				throw(new Error((("名称为" + assetsKey) + "已经存在。")));
			}
			this._assets[assetsKey] = url;
		}

		/**
		 * 资源池中是否存在对象
		 * @param	key
		 * @return
		 */
		public function hasAssets(key:String):Boolean {
			var tempValue:Boolean = this._assets[key] ? true : false;
			return tempValue;
		}

		/**
		 * 获取资源路径
		 * @param	assetsKey	资源键
		 * @return
		 */
		public function getAssetsUrl(assetsKey:String):String {
			if (this.hasAssets(assetsKey) == false) {
				throw(new Error("对象 _assets 找不到键名为 " + assetsKey + " 的项。"));
			}
			if (assetsKey.indexOf(URI.assetsUrl) == 0) {
				return assetsKey;
			}
			return (URI.assetsUrl + this._assets[assetsKey]);
		}

		/**
		 * 获取资源对象File
		 * @param	key
		 * @return
		 */
		public function getAssetsObj(key:String):File {
			return (this._libs[key] as File);
		}

		/**
		 * 加载资源
		 * @param	assetList	资源键列表
		 * @param	responder	加载响应对象
		 * @param	fullURL		是否为全局路径
		 */
		public function loadResources(assetList:Array, responder:LoadResponder, fullURL:Boolean = false):void {
			var len:int;
			var i:int;
			var list:Array = assetList;
			var loadResponder:LoadResponder = responder;
			var fullUrl:Boolean = fullURL;
			var callback:Function = function(_arg1:Array):void {
				oObject.list(_arg1, _libs, list);
				loadResponder.lCallback();
			}
			var progress:Function = function(_arg1:int, _arg2:int, _arg3:int, _arg4:String):void {
				loadResponder.lProgress(list[_arg2], _arg3, _arg2, _arg1, _arg4);
			}
			var tempList:Array = [];
			len = list.length;
			i = 0;
			while (i < len) {
				tempList[i] = (fullUrl) ? list[i] : this.getAssetsUrl(list[i]);
				i++;
			}
			File.loadList(tempList, callback, progress);
		}

		/**
		 * 获取资源类引用
		 * @param	key		键
		 * @param	name	类名
		 * @return
		 */
		public function getAssetsClass(key:String, name:String):Class {
			if (this.hasResource(key) == false) {
				throw new Error("找不到名为 " + key + " 的资源。请确认资源 " + key + " 关联的swf文件已经加载。");
			}
			return (this._libs[key] as File).getClassByName(name);
		}

		/**
		 * 获取资源对象
		 * @param	key		键
		 * @param	name	类名
		 * @return
		 */
		public function getAssetsObject(key:String, name:String):Object {
			var classDef:Class = (this.getAssetsClass(key, name) as Class);
			return new (classDef)();
		}

		/**
		 * 检查加载完后的对象列表中是否有对应的资源
		 * @param	key		键
		 * @return
		 */
		public function hasResource(key:String):Boolean {
			var tempValue:Boolean = (this._libs[key]) ? true : false;
			return tempValue;
		}

		/**
		 * 从加载完后的资源列表中删除对象
		 * @param	key
		 */
		public function removeResource(key:String):void {
			if (this.hasResource(key)) {
				delete this._libs[key];
			}
		}

		public function center(_arg1:String, _arg2:DisplayObject, _arg3:Function = null):void {
			var sign:String = _arg1;
			var d:DisplayObject = _arg2;
			var func:Function = _arg3;
			if (func == null) {
				func = function():void {
					if (d.stage == null) {
						removeFromPositionList(sign);
						return;
					}
					var _local1:int = Structure.stageWidth;
					var _local2:int = Structure.stageHeight;
					var _local3:Number = Math.floor(((_local2 - d.height) / 2));
					d.x = Math.floor((_local1 - d.width) / 2);
					d.y = (_local3 < 0) ? 60 : _local3;
				}
			}
			func();
			this.addToPositionList(sign, func);
		}

		public function addToPositionList(_arg1:String, _arg2:Function):void {
			this._positionList[_arg1] = _arg2;
		}

		public function removeFromPositionList(_arg1:String):void {
			delete this._positionList[_arg1];
		}

		public function cancelCenter(_arg1:String):void {
			this.removeFromPositionList(_arg1);
		}

		public function get control():Controller {
			return _control;
		}

	}

}
